<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>
        人员列表
    </title>
    <meta content="webkit" name="renderer">
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport">
    <meta content="black" name="apple-mobile-web-app-status-bar-style">
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta content="telephone=no" name="format-detection">
    <link href="css/main.css" media="all" rel="stylesheet">
    <link href="css/bootstrap.css" rel="stylesheet">
</head>
<body>
<div class="x-nav">
            <span class="layui-breadcrumb">
              <a><cite>首页</cite></a>
              <a><cite>业主信息管理</cite></a>
              <a><cite>人员列表</cite></a>
            </span>
    <a class="layui-btn layui-btn-small" href="javascript:location.replace(location.href);"
       style="line-height:1.6em;margin-top:3px;float:right" title="刷新"><i class="layui-icon"
                                                                            style="line-height:30px">ဂ</i></a>
</div>
<div class="x-body">
    <form action="" class="layui-form x-center" style="width:800px">
        <div class="layui-form-pane" style="margin-top: 15px;">
            <div class="layui-form-item">
                <div class="layui-input-inline">
                    <select lay-filter="paymentStatus" name="paymentStatus">
                        <option value="">全部状态</option>
                        <option value="1">已缴费</option>
                        <option value="0">未缴费</option>
                    </select>
                </div>
                <!--                <div class="layui-input-inline">-->
                <!--                    <input autocomplete="off" class="layui-input" name="username" placeholder="请输入搜索关键词"-->
                <!--                           type="text">-->
                <!--                </div>-->
                <div class="layui-input-inline" style="width:80px">
                    <button class="layui-btn" lay-filter="sreach" onclick="searchData()"><i
                            class="layui-icon">&#xe615;</i>搜索
                    </button>
                </div>
            </div>
        </div>
    </form>
    <xblock>
        <!--        <button class="layui-btn layui-btn-danger" onclick="delAll()">-->
        <!--            <i class="layui-icon">&#xe640;</i>批量删除-->
        <!--        </button>-->
        <!--        <button class="layui-btn" onclick="x_admin_show('添加业主','ownerAdd.html','1000','600')">-->
        <!--            <i class="layui-icon">&#xe608;</i>添加-->
        <!--        </button>-->
        <span class="x-right" id="ownerCount" style="line-height:40px">共有数据：<span
                class="layui-badge">0</span> 条</span>
    </xblock>
    <table class="layui-table">
        <thead>
        <tr>
            <th>
                姓名
            </th>
            <th>
                联系电话
            </th>
            <th>
                名下房产
            </th>
            <th>
                照片
            </th>
            <th>
                所属单元
            </th>
            <th>
                所属楼栋
            </th>
            <th>
                业主信息创建时间
            </th>
            <th>
                缴费周期
            </th>
            <th>
                缴费时间
            </th>
            <th>
                是否缴费
            </th>
            <th>
                截至今日是否到期
            </th>
            <th>
                操作
            </th>
        </tr>
        </thead>
        <tbody id="x-img">

        </tbody>
    </table>

    <!--    <div id="page">-->
    <!--        <ul class="pagination">-->
    <!--            <li class="disabled"><span>&laquo;</span></li>-->
    <!--            <li class="active"><span>1</span></li>-->
    <!--            <li><a href="">2</a></li>-->
    <!--            <li><a href="">3</a></li>-->
    <!--            <li><a href="">&raquo;</a></li>-->
    <!--        </ul>-->
    <!--    </div>-->
</div>
<script charset="utf-8" src="lib/layui/layui.js"></script>
<script charset="utf-8" src="js/x-layui.js"></script>
<script>
    layui.use(['laydate', 'element', 'laypage', 'layer'], function () {
        $ = layui.jquery;//jquery
        laydate = layui.laydate;//日期插件
        lement = layui.element();//面包导航
        laypage = layui.laypage;//分页
        layer = layui.layer;//弹出层

        //以上模块根据需要引入
        layer.ready(function () { //为了layer.ext.js加载完毕再执行
            layer.photos({
                photos: '#x-img'
                //,shift: 5 //0-6的选择，指定弹出图片动画类型，默认随机
            });
        });
        var start = {
            min: laydate.now()
            , max: '2099-06-16 23:59:59'
            , istoday: false
            , choose: function (datas) {
                end.min = datas; //开始日选好后，重置结束日的最小日期
                end.start = datas //将结束日的初始值设定为开始日
            }
        };

        var end = {
            min: laydate.now()
            , max: '2099-06-16 23:59:59'
            , istoday: false
            , choose: function (datas) {
                start.max = datas; //结束日选好后，重置开始日的最大日期
            }
        };

        document.getElementById('LAY_demorange_s').onclick = function () {
            start.elem = this;
            laydate(start);
        }
        document.getElementById('LAY_demorange_e').onclick = function () {
            end.elem = this
            laydate(end);
        }
    });

    //批量删除提交
    function delAll() {
        layer.confirm('确认要删除吗？', function (index) {
            //捉到所有被选中的，发异步进行删除
            layer.msg('删除成功', {icon: 1});
        });
    }

    function question_show(argument) {
        layer.msg('可以跳到前台具体问题页面', {icon: 1, time: 1000});
    }

    /*添加*/
    function personnel_add(title, url, w, h) {
        x_admin_show(title, url, w, h);
    }

    //编辑
    function personnel_edit(title, url, id, w, h) {
        url = url + "?id=" + id;
        x_admin_show(title, url, w, h);
    }

    /*删除*/
    function personnel_del(obj, id) {
        layer.confirm('确认要删除吗？', {icon: 3, title: '提示信息'}, function (index) {
            $.ajax({
                type: "post",
                url: "xxx",
                data: {id: id},
                dataType: "json",
                success: function (data) {
                    //console.log(data);
                    if (data.status == 1) {
                        //发异步删除数据
                        $(obj).parents("tr").remove();
                        layer.msg(data.info, {icon: 6, time: 1000});
                        setTimeout(function () {
                            window.location.reload();
                        }, 1000);
                        return false;
                    } else {
                        layer.msg(data.info, {icon: 5, time: 1000});
                        return false;
                    }
                }
            });
            //发异步删除数据
            $(obj).parents("tr").remove();
            layer.msg('已删除!', {icon: 1, time: 1000});
        });
    }
</script>

</body>
<script src="js/jquery-3.7.1.min.js" type="text/javascript"></script>
<script src="/js/request.js"></script>
<script>

    function loadOwnerData() {
        $.ajax({
            method: 'get',
            url: '/admin/owner/detailById',
            // url: '/admin/owner/infoById',
            data: {id: sessionStorage.getItem('userId')},
            success: function (res) {
                let html = '';
                for (let i = 0; i < res.data.length; i++) {
                    let temp = res.data[i];
                    html += `<tr>
                    <td>${temp.name}</td>
                    <td>${temp.phone}</td>
                    <td>${temp.houseName}</td>
                    <td>${temp.photoUrl == null ? '暂无' : `<img height="30" src="${temp.photoUrl}" width="50">`}</td>
                    <td>${temp.unitName}</td>
                    <td>${temp.buildingName}</td>
                    <td>${formatDate(temp.createdTime)}</td>
                    <td>${temp.paymentPeriod}</td>
                    <td>${formatDate(temp.paymentTime)}</td>
                    <td>${temp.isPay == null || temp.isPay == 0 ? '未缴费' : '已缴费'}</td>
                    <td>${isExpired(temp.paymentPeriod, temp.paymentTime) ? '<span style="color:red">已到期</span>' : '<span style="color:blue">未到期</span>'}</td>
                    <td class="td-manage">
                        <a class="ml-5" href="javascript:;" onclick="sendEmail('${temp.userId}')" title="发送邮件">
                            <i class="layui-icon">&#xe618;</i>
                        </a>
                    </td>
                </tr>`;
                }

                $('#x-img').html(html);
                $('#ownerCount').html('共有数据：<span class="layui-badge">' + res.data.length + '</span> 条');

                // 重新渲染表单元素
                layui.form.render();
            }
        });
    }

    function searchData() {
        // const keyword = $('input[name="username"]').val().toLowerCase();
        const paymentStatus = $('select[name="paymentStatus"]').val();

        let filteredData = allData;

        // 按缴费状态筛选
        if (paymentStatus !== '') {
            filteredData = filteredData.filter(item =>
                item.isPay == paymentStatus
            );
        }

        // 按关键词筛选
        // if (keyword) {
        //     filteredData = filteredData.filter(item =>
        //         item.name.toLowerCase().includes(keyword) ||
        //         item.phone.includes(keyword) ||
        //         (item.houseName && item.houseName.toLowerCase().includes(keyword)) ||
        //         (item.unitName && item.unitName.toLowerCase().includes(keyword)) ||
        //         (item.buildingName && item.buildingName.toLowerCase().includes(keyword))
        //     );
        // }

        currentPage = 1;
        renderTable(filteredData);
        updatePagination();
    }

    $(function () {

        loadOwnerData();

        layui.form.render('select');

        // 表单验证
        layui.form.verify({
            required: function (value) {
                if (value.length == 0) {
                    return '必填项不能为空';
                }
            },
            phone: function (value) {
                if (!/^1[3-9]\d{9}$/.test(value)) {
                    return '请输入正确的手机号码';
                }
            }
        });

        /*$.ajax({
            method: 'get',
            url: '/admin/owner/detail',
            success: function (res) {
                console.log(res);
                let html = '';
                for (let i = 0; i < res.data.length; i++) {
                    let temp = res.data[i];
                    html += `<tr>
            <td>
                <input name="" type="checkbox" value="1">
            </td>
            <td>
                ${temp.userId}
            </td>
            <td>
                ${temp.name}
<!--                <u onclick="question_show()" style="cursor:pointer">-->
<!--                    金华万府 </u>-->
            </td>
            <td>
                ${temp.phone}
            </td>
            <td>
                ${temp.houseName}
            </td>
            <td>
                ${temp.photoUrl == null ? '暂无' : `<img height="30" src="${temp.photoUrl}" width="50">`}
            </td>
            <td>
                ${temp.unitName}
            </td>
            <td>
                ${temp.buildingName}
            </td>
            <td>
                ${temp.managerName}
            </td>
            <td>
                ${formatDate(temp.createdTime)}
            </td>
            <td>
                ${temp.isPay == 0 ? '未缴费' : '已缴费'}
            </td>
            <td class="td-manage">
                <a class="ml-5" href="javascript:;"
                   onclick="personnel_edit('编辑','personnel_add.html','4','1000','600')"
                   style="text-decoration:none" title="编辑">
                    <i class="layui-icon">&#xe642;</i>
                </a>
                <a href="javascript:;" onclick="personnel_del(this,'4')" style="text-decoration:none"
                   title="删除">
                    <i class="layui-icon">&#xe640;</i>
                </a>
            </td>
        </tr>`;

                }
                $('#x-img').html(html);
                $('#ownerCount').html('共有数据：<span class="layui-badge">' + res.data.length + '</span> 条');
            }
        })*/
    })

    function member_del(obj, id) {
        layer.confirm('确认要删除吗？', function (index) {
            $.ajax({
                type: "post",
                url: '',
                data: {id: id},
                success: function (res) {
                    if (res.code == 200) {
                        $(obj).parents("tr").remove();
                        layer.msg('已删除!', {icon: 1, time: 1000});
                        loadOwnerData();
                    } else {
                        layer.msg(res.msg, {icon: 5, time: 1000});
                    }
                }
            });
        });
    }

    function sendEmail(userId) {
        layer.prompt({
            title: '输入邮件内容',
            formType: 2 // 多行文本
        }, function (text, index) {
            layer.close(index);
            // 这里添加发送邮件的AJAX请求
            $.ajax({
                url: '/admin/sendEmail',
                type: 'POST',
                data: {
                    userId: userId,
                    content: text
                },
                success: function (res) {
                    if (res.code === 200) {
                        layer.msg('邮件发送成功', {icon: 1});
                    } else {
                        layer.msg(res.msg || '发送失败', {icon: 2});
                    }
                }
            });
        });
    }


    function formatDate(dateStr) {
        if (dateStr == null) {
            return "";
        }
        const date = new Date(dateStr);
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        return `${year}-${month}-${day}`;
    }

    // 判断是否到期
    function isExpired(period, paymentTime) {
        if (paymentTime == null) {
            return true;
        }
        if (!period || !paymentTime) return true;

        // 提取月份数
        const months = parseInt(period.replace('个月', ''));
        if (isNaN(months)) return true;

        // 计算到期时间
        const paymentDate = new Date(paymentTime);
        const expireDate = new Date(paymentDate.setMonth(paymentDate.getMonth() + months));

        // 比较当前时间与到期时间
        return new Date() > expireDate;
    }
</script>
</html>